// pages/home/home.js
import CommonService from '../../service/CommonService.js';
import { throttle } from '../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [],
    count: {},
    adList: [],
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.commonService = new CommonService();
    const type = options.type;
    this.setData({ user: app.globalData.user })
    if(type){
      this.setData({ type, types: app.globalData.productTypes })
    }else{
      this.setData({ types: app.globalData.productTypes })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _ = this;
    this.commonService.homeCount().then(function(result){
      if(result.status == 0){
        _.setData({count: result.data});
      }
    })
    const param = { state: 1, type: 3 };
    this.commonService.listAd(param).then(function (result) {
      if (result.status == 0) {
        _.setData({ adList: result.data });
      }
    })
  },
  onTypeTap:function(e){
    const { type } = this.data;
    throttle(function () {
      const user = app.globalData.user;
      if (user.userType == 1 && user.authStatus == 2) {
        wx.navigateTo({
          url: '/packageFactory/pages/factory/factory-list?productType='+e.detail.id
        })
      } else if (user.userType == 2 && user.authStatus == 2) {
        wx.navigateTo({
          url: '/packageOrder/pages/order/order-list?productType=' + e.detail.id
        })
      } else {
        if (type == 'order') {
          wx.navigateTo({
            url: '/packageOrder/pages/order/order-list?productType=' + e.detail.id
          })
        } else if (type == 'factory') {
          wx.navigateTo({
            url: '/packageFactory/pages/factory/factory-list?productType=' + e.detail.id
          })
        }
      }

    }, 1000)();
  },
  toSearch: function(){
    const { type } = this.data;
    throttle(function () {
      if (type == 'order') {
        wx.navigateTo({
          url: '/packageOrder/pages/order/order-list'
        })
      } else if (type == 'factory') {
        wx.navigateTo({
          url: '/packageFactory/pages/factory/factory-list'
        })
      }

    },1000)();
  },
  onUserTap: function (e) {
    throttle(function () {
      const { user } = app.globalData;
      console.log(user);
      if (user.authStatus != 2) {
        wx.navigateTo({
          url: '/packageUser/pages/auth/auditting'
        })
      } else {
        wx.navigateTo({
          url: '/packageUser/pages/user/user'
        })
      }
    }, 1000)()
  },
  onCreateOrder: throttle(function (e) {
    wx.navigateTo({
      url: '/packageOrder/pages/order/create-order'
    })
  }, 1000),
  toRuZhu: function (e) {
    wx.showActionSheet({
      itemList: ['工厂方认证', '订单方认证'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          wx.navigateTo({
            url: '/packageUser/pages/auth/auth-rules'
          })
        } else {
          wx.navigateTo({
            url: '/packageUser/pages/auth/business-auth'
          })
        }

      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  }
})